using HarmonyLib;
using ItemStatsSystem.Items;

// ReSharper disable ConvertToConstant.Local

namespace IncreaseWeaponSlots
{
    public enum SlotSetting
    {
        [EnumDisplay("设置 - 将槽位设置为6个")] Set = 0,

        [EnumDisplay("增加 - 增加缺失的槽位，已有槽位不变")] Add = 1
    }

    public enum SlotRequireSetting
    {
        [EnumDisplay("通用 - 槽位不限制配件类型")] General = 0,

        [EnumDisplay("约束 - 槽位只能装备与武器相关的类型")] Require = 1
    }

    [HarmonyPatch(typeof(SlotCollection), "OnInitialize")]
    public class SlotCollectionOnInitializePatch
    {
        [Config(Description = "插槽处理行为", ConfigName = "slotSetting")]
        private static readonly SlotSetting SlotSetting = SlotSetting.Set;

        [Config(Description = "是否将原生6槽位以上武器的槽位设置为通用槽位？", ConfigName = "setSix")]
        private static readonly bool SetSix = false;

        [Config(Description = "槽位是否约束配件类型", ConfigName = "slotRequireSetting")]
        private static readonly SlotRequireSetting SlotRequireSetting = SlotRequireSetting.General;

        [HarmonyPrefix]
        // ReSharper disable once InconsistentNaming
        public static bool Prefix(SlotCollection __instance)
        {
            try
            {
                var item = __instance.Master;
                if (item && item.Tags.Contains("Gun"))
                {
                    if (__instance.Count < 6)
                    {
                        var typeTag = SlotRequireSetting == SlotRequireSetting.General
                            ? null
                            : IncreaseWeaponSlotsUtils.GetTypeTag(item);
                        
                        IncreaseWeaponSlotsUtils.LogItemInfo(item);
                        
                        if (SlotSetting == SlotSetting.Set)
                        {
                            IncreaseWeaponSlotsUtils.SetSlotList(__instance, typeTag);
                        }
                        else
                        {
                            IncreaseWeaponSlotsUtils.AddSlotList(__instance, typeTag);
                        }
                    }
                    else if (SetSix)
                    {
                        IncreaseWeaponSlotsUtils.LogItemInfo(item);
                        IncreaseWeaponSlotsUtils.SetAllSlot(__instance);
                    }
                }
            }
            catch (System.Exception ex)
            {
                ModLog.Error($"{ex.Message}");
            }

            return true;
        }
    }
}